/**************************************************************************
 * @doc CORE
 * @module retry.C |  
 *      Functions to Control Retry processing.
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   
 * <nl>Notes       :
 *
 * @head3 CORE Functions |
 * @index | CORE
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 * 
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form 
 * or by any means whether electronic, mechanical, magnetic, optical, 
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log:  $
 * ------------------------------------------------------------------------
 ********************************************************************************/
#include <project.h>
#define RETRY_C

#include <stdio.h>
#include <string.h>



#ifdef DEBUG_RETRY
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <comm.h>
#include <confproto.h>
#include <entry.h>
#include <msg.h>
#include <msgfuncs.h>
#include <retry.h>
#include <ui.h>
#include <spdebug.h>
#include <codesec.h>
#include <trt.h>

/********************************************************************************************* 
* @func void | vdGetRetryFileName | 
*       Get the retry file name
*
* @parm char * | pszFName | 
*       Buffer to hold name of retry file
*
* @rdesc return values.
* @end
**********************************************************************************************/
/*
void vdGetRetryFileName (char *pszFName)
{
    sprintf(pszFName, "pobtran.ret");
}
*/

/********************************************************************************************* 
* @func int | inSaveRetryRequest | 
*       Save pobTran for the retry into the retry file
*
* @parm TRANSACTION_OBJECT * |pobTran | 
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS
* @flag int    |  VS_ERR
* @end
**********************************************************************************************/

int inSaveRetryRequest(TRANSACTION_OBJECT* pobTran) {
    int inFlHandle;
    /*  char    szRetryFile[FILENAME_SIZE + 1];*/



    /* Save the retry record in the retry file*/
    if ((inFlHandle = open(RETRY_FILE_NAME, O_RDWR | O_CREAT)) < 0)
        return(VS_ERR);

    if ((write(inFlHandle, (char *) pobTran, sizeof(TRANSACTION_OBJECT))) <= 0) {
        close(inFlHandle);
        return(VS_ERR);
    }

    if (close(inFlHandle) < 0)
        return(VS_ERR);

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func void | vdCancelPendingRetry | 
*       Delete the retry file
*
* @rdesc return values.
* @end
**********************************************************************************************/

void vdCancelPendingRetry() {
    remove(RETRY_FILE_NAME);
}





/********************************************************************************************* 
* @func int | inReadRetryRequest | 
*       Read the retry record from the retry file into pobTran
*
* @parm TRANSACTION_OBJECT * |pobTran | 
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS
* @flag int    |  VS_ERR
* @end
**********************************************************************************************/

int inReadRetryRequest(TRANSACTION_OBJECT* pobTran) {
    int inFlHandle;


    /* Read the retry record from the retry file into pobTran */
    if ((inFlHandle = open(RETRY_FILE_NAME, O_RDWR)) < 0)
        return(VS_ERR);

    if ((read(inFlHandle, (char *) pobTran, sizeof(TRANSACTION_OBJECT))) != sizeof(TRANSACTION_OBJECT)) {
        close(inFlHandle);
        return(VS_ERR);
    }

    if (close(inFlHandle) < 0)
        return(VS_ERR);

    /* Must set fRetry because saved retry request did not have flag set */   
    pobTran->fRetry = VS_TRUE;
    pobTran->fPredialed = VS_FALSE;


    return(VS_SUCCESS);
}
/********************************************************************************************* 
* @func int | inRetryTransaction | 
*       Prompt user to re-transmit the transaction
*
* @parm TRANSACTION_OBJECT * |pobTran | 
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS
* @flag int    |  VS_ERR
* @end
**********************************************************************************************/

int inRetryTransaction(TRANSACTION_OBJECT* pobTran) {
    char szRespMsg[RESP_MSG_SIZE + 1];
    signed char chAnswer;
    int inStatus;

    vdGGRespMsg(szRespMsg);

    //if (pobTran->srTRTRec.fRetryPrompt == VS_TRUE && pobTran->fRetry == VS_TRUE)
    if (fGetRetryPrompt(pobTran) == VS_TRUE && pobTran->fRetry == VS_TRUE) {
        chAnswer = (char) obDisp.inDO_ChooseOne(szRespMsg, (char *) pchGetMsg(RETRY_PROMPT_MSG15), (char *) pchGetMsg(YESNO_MSG5x), 2, NULL_PFI);
        if (chAnswer == KEY_YES) {
            SVC_WAIT(1000); /* Wait for modem comm to complete end */
            if (inReadRetryRequest(pobTran) == VS_ERR)
                return(VS_ERR);
            vdSGErrorMessage(0L);         /* Clear error message index */
            vdDisplayMessageFromFile(PROCESSING_MSG21, STATUS_MSG);
            if (obCommunication.inBegin) {
                inStatus = obCommunication.inBegin(&obCommunication, shGetCommunicationIndex(), VS_TRUE);

                if (inStatus)
                    return(inStatus);
            }
            SVC_WAIT(1500);

            if (obCommunication.inCheck) {
                if (obCommunication.inCheck(&obCommunication) != VS_SUCCESS)
                    return(VS_ERR);
            }
            vdCancelPendingRetry();
            return (VS_SUCCESS);
        }
    }
    return(VS_ESCAPE);
}
int RETRY_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}



#undef RETRY_C

